﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            NewMethod();


        }
    }

    private void NewMethod()
    {
        List<Product> list = new List<Product>();
        for (int i = 0; i < 3; i++)
        {
            Product p = new Product();
            p.Id = i;
            p.Name = string.Format("Name{0}", i);
            list.Add(p);
        }


        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TemplateField tf = this.GridView1.Columns[0] as TemplateField;
        IBindableTemplate bt = tf.ItemTemplate as IBindableTemplate;

        GridViewRow r = this.GridView1.Rows[e.RowIndex];


        System.Collections.Specialized.IOrderedDictionary dict
            = bt.ExtractValues(r);


        //System.Collections.Specialized.IOrderedDictionary dict
        //    = this.ExtractValuesFromRow(this.GridView1, e.RowIndex);

        string name = dict["Name"] as string;

    }

    private System.Collections.Specialized.IOrderedDictionary ExtractValuesFromRow(
        GridView gv,
        int rowIndex
        )
    {
        System.Collections.Specialized.OrderedDictionary dict
            = new System.Collections.Specialized.OrderedDictionary();

        GridViewRow r = gv.Rows[rowIndex];

        for (int i = 0; i < gv.Columns.Count; i++)
        {
            DataControlField column = gv.Columns[i];

            System.Collections.Specialized.OrderedDictionary d
                = new System.Collections.Specialized.OrderedDictionary();
            column.ExtractValuesFromCell(d, r.Cells[i] as DataControlFieldCell, r.RowState, true);

            foreach (System.Collections.DictionaryEntry entry in d)
            {
                dict.Add(entry.Key, entry.Value);
            }
        }
        return dict;
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        NewMethod();

    }
}